//
//  DSRachViewController.m
//  DS爱信
//
//  Created by 秦建英 on 14-5-27.
//  Copyright (c) 2014年 秦建英. All rights reserved.
//

#import "DSRachViewController.h"
#import "NetAccess.h"
@interface DSRachViewController ()

@end

@implementation DSRachViewController
{
    UITextField *_field;
    UITextField *_field1;
    UITextField *_field2;
    NSMutableData *_data;
    UIAlertView *_alertView;
    NSString *_string;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    _field.text =nil;
    _field1.text =nil;
    _field2.text =nil;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTitleviewWithTitle:@"实惠充充"];
    self.view.backgroundColor =[UIColor whiteColor];
    UIButton *bbtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    bbtn.frame =CGRectMake(0, 0, 70, 30);
    [bbtn setTitle:@"<<返回" forState:UIControlStateNormal];
    [bbtn setTintColor:[UIColor blackColor]];
    [bbtn addTarget:self action:@selector(bbiClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bbtn];
    UIBarButtonItem *right =[[UIBarButtonItem alloc]initWithCustomView:bbtn];
    self.navigationItem.leftBarButtonItem =right;
    [self textfield];
    UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainicon.png"]];
    imageView.frame =CGRectMake(60, 0, 200, 100);
    [self.view addSubview:imageView];
    
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn .frame =CGRectMake(20, _field2.frame.size.height+230, 280, 50);
    btn.backgroundColor =[UIColor colorWithRed:0.2f green:0.6f blue:0.9f alpha:1];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"确定充值" forState:UIControlStateNormal];
    btn.layer.cornerRadius =10;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}
-(void)bbiClick
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}
-(void)btnClick
{
    NSString *message = @"注册成功";
    // 提示框
    _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    
    if(_field.text.length <= 0)
    {
        message = @"亲！您的卡号不存在..";
        _alertView.message = message;
        // 显示提示框
        [_alertView show];
        return;
    }
    if(_field1.text.length <= 0)
    {
        message = @"亲~你的充值卡号输入不正确哦！";
        _alertView.message = message;
        [_alertView show];
        return;
    }
    if(_field2.text.length <= 0)
    {
        message = @"亲~密码不正确哦！";
        _alertView.message = message;
        [_alertView show];
        return;
    }
    if(_field.text.length>0&&_field1.text.length>0&&_field2.text.length>0)
    {
        NetAccess *net =[NetAccess sharedNetAccess];
        if(![net haveNet])
        {
            NSLog(@"网络不可用");
            //没有网络
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
        }else{
            NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://182.92.157.109/api/mapp/paybycard.aspx?a=%@&b=%@&c=%@",_field.text,_field1.text,_field2.text]];
            NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
            NSURLConnection *connent =[[NSURLConnection alloc]initWithRequest:request delegate:self];
            [connent start];
            NSLog(@"enen ");
        }
        return;
    }
}
#pragma mark--网络代理方法
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    _data =[[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    _string =[[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding];
    NSLog(@"dict ==%@",_string);
    
    if([_string hasPrefix:@"0|1"])
    {
        NSString *message =@"亲！恭喜充值成功哦！";
        _alertView.message = message;
        [_alertView show];
    }
    else
    {
        NSString *message =@"亲！电话卡密码错误！";
        _alertView.message = message;
        [_alertView show];
    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSString *message =@"亲！网络不给力啊，稍后再试吧..!";
    _alertView.message = message;
    [_alertView show];
}
-(void)textfield
{
    _field =[[UITextField alloc]initWithFrame:CGRectMake(20,110, 320-40,40)];
    _field.delegate =self;
    _field.borderStyle = UITextBorderStyleLine;
    _field.layer.cornerRadius =10;
    _field.placeholder = @"手机号码:";
    [self.view addSubview:_field];
    
    _field1 =[[UITextField alloc]initWithFrame:CGRectMake(20,160, 320-40,40)];
    _field1.delegate =self;
    _field1.borderStyle =UITextBorderStyleLine;
    _field1.layer.cornerRadius =10;
    _field1.placeholder = @"充值卡卡号:";
    [self.view addSubview:_field1];
    
    _field2 =[[UITextField alloc]initWithFrame:CGRectMake(20,210, 320-40,40)];
    _field2.delegate =self;
    _field2.borderStyle =UITextBorderStyleLine;
    _field2.layer.cornerRadius =10;
    _field2.secureTextEntry =YES;
    _field2.placeholder = @"充值卡密码:";
    [self.view addSubview:_field2];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_field resignFirstResponder];
    [_field1 resignFirstResponder];
    [_field2 resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
